<template>
  <div id="UserManagement">
    <template>
      <el-table
        ref="singleTable"
        :data="tableData"
        highlight-current-row
        style="width: 100%"
        @selection-change="handleSelectionChange">
<!--        <el-table-column-->
<!--          type="selection"-->
<!--          width="55">-->
<!--        </el-table-column>-->
<!--        用户编号-->
        <el-table-column
          type="index"
          label="用户编号"
          min-width="80"
          width="80">
        </el-table-column>
<!--        用户名称-->
        <el-table-column
          prop="account"
          label="账号"
          min-width="180"
          width="auto">
        </el-table-column>
<!--        用户昵称-->
        <el-table-column
          prop="nickname"
          label="用户昵称"
          min-width="180"
          width="auto">
        </el-table-column>
<!--        邮箱-->
        <el-table-column
          prop="email"
          label="邮箱"
          min-width="180"
          width="auto">
        </el-table-column>
<!--        状态-->
        <el-table-column
          prop="state"
          label="状态"
          min-width="60"
          width="auto">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.judegState"
              active-color="#ff9512"
              inactive-color="#dcdfe6"
              disabled>
            </el-switch>
          </template>
        </el-table-column>
<!--        创建时间-->
        <el-table-column
          prop="registerTime"
          label="创建时间"
          min-width="180"
          width="auto">
        </el-table-column>
<!--        操作-->
        <el-table-column
          prop="operation"
          label="操作"
          width="180">
          <template slot-scope="scope">
            <a class="edit" @click="selectUser(scope.row.userId,scope.$index)"><i class="el-icon-edit"></i>查看</a>
<!--            <a class="delete"><i class="el-icon-delete"></i>查看</a>-->
<!--            <a class="arrow">-->
<!--              <i class="el-icon-d-arrow-right"></i>-->
<!--              更多-->
<!--              <div class="more">-->
<!--                <a href=""><i class="el-icon-key"></i>重置密码</a>-->
<!--              </div>-->
<!--            </a>-->
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      class="pages"
      background
      layout="prev, pager, next"
      :total="pages.total"
      :current-page="pages.current"
      :pager-count=5
      @current-change="handleCurrentChange">
    </el-pagination>
    <div class="cover" v-if="msg.select">
      <div class="middle">
        <div class="delete" @click="notShow">×</div>
        <el-form ref="form" :model="form" label-width="80px">
          <el-avatar :size="60" :src="form.headImg" @error="errorHandler">
            <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"/>
          </el-avatar>
          <el-form-item label="账号:">
            <el-input v-model="form.account" disabled></el-input>
          </el-form-item>
          <el-form-item label="昵称:">
            <el-input v-model="form.nickname" disabled></el-input>
          </el-form-item>
          <el-form-item label="性别:">
            <el-input v-model="form.sexNum" disabled></el-input>
          </el-form-item>
          <el-form-item label="邮箱:">
            <el-input v-model="form.email" disabled></el-input>
          </el-form-item>
          <el-form-item label="创建时间:">
            <el-input v-model="form.registerTime" disabled></el-input>
          </el-form-item>
          <el-form-item label="状态:">
            <span class="stateSpan">
              <el-input v-model="form.stateNum" disabled></el-input>
            </span>
            <el-button @click="ban" class="stateBtn">{{form.stateNum?'封禁用户':'解除封禁'}}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import cookie from '../cookien'
export default {
  name: 'UserManagement',
  data () {
    return {
      tableData: [
        // {
        //   number: 1,
        //   date: '2016-05-02',
        //   name: '王小虎',
        //   nick: '王小虎nick',
        //   email: '2634917964@qq.com',
        //   state: true,
        //   address: '上海市普陀区金沙江路 1518 弄',
        //   choose: false,
        //   id: 1
        // }
      ],
      // 分页
      pages: {
        // 每页十条，共几条
        total: 0,
        // 当前页数
        vals: 1,
        // 自定义第几页
        current: 1
      },
      msg: {
        select: false
      },
      arr: '',
      form: {
        // account: 'lishoujia',
        // address: null,
        // attention: 0,
        // birthday: null,
        // email: null,
        // grade: 0,
        // headImg: null,
        // like: null,
        // nickname: '栗守佳',
        // realName: null,
        // regisrerTime: '2022-02-16',
        // sex: 0,
        // state: 0,
        // userId: 2
      }
    }
  },
  methods: {
    // 头像
    errorHandler () {
      return true
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleCurrentChange (val) {
      // 当前页数
      this.pages.vals = val
      this.$axios({
        url: '/Menu/Admin/selectUserByPage',
        headers: {
          token: cookie.getToken()
        },
        params: {
          pageStart: this.pages.vals,
          pageSize: 10
        }
      }).then(data => {
        if (data.data.code === 200) {
          for (var i = 0; i < data.data.object.list.length; i++) {
            // data.data.object.list[i].judegState = (data.data.object.list[i].attention === 0 ? true : false)
            if (data.data.object.list[i].state === 0) {
              data.data.object.list[i].judegState = true
            } else {
              data.data.object.list[i].judegState = false
            }
          }
          this.pages.total = data.data.object.total
          this.tableData = data.data.object.list
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 关闭弹窗
    notShow () {
      this.msg.select = !this.msg.select
    },
    ban () {
      if (this.form.stateNum === '正常') {
        this.banUser(1, this.form.userId)
      } else {
        this.banUser(0, this.form.userId)
      }
    },
    banUser (state, userId) {
      this.$axios({
        url: '/Menu/Admin/banUserId',
        params: {
          state,
          userId
        }
      }).then(data => {
        if (data.data.code === 200) {
          this.$message(data.data.message)
          this.tableData[this.arr].judegState = !this.tableData[this.arr].judegState
          this.msg.select = false
        } else {
          this.$message(data.data.message)
        }
      }).catch(err => {
        console.log(err.toString())
      })
    },
    // 查询用户
    selectUser (id, index) {
      this.arr = index
      this.$axios({
        url: '/Menu/Admin/selectByUserId',
        headers: {
          token: cookie.getToken()
        },
        params: {
          userId: id
        }
      }).then(data => {
        const msg = data.data
        console.log(msg)
        if (msg.code === 200) {
          this.form = msg.object
          // 男女
          if (this.form.sex === 0) {
            this.form.sexNum = '男'
          } else {
            this.form.sexNum = '女'
          }
          // 状态
          if (this.form.state === 0) {
            this.form.stateNum = '正常'
          } else {
            this.form.stateNum = '封禁'
          }
          this.msg.select = !this.msg.select
        } else {
          this.$message(msg.message)
        }
      }).catch(err => {
        console.log(err)
      })
    }
  },
  computed: {
  },
  mounted () {
    this.$axios({
      url: '/Menu/Admin/selectUserByPage',
      headers: {
        token: cookie.getToken()
      },
      params: {
        pageStart: 1,
        pageSize: 10
      }
    }).then(data => {
      if (data.data.code === 200) {
        for (var i = 0; i < data.data.object.list.length; i++) {
          // data.data.object.list[i].judegState = (data.data.object.list[i].attention === 0 ? true : false)
          if (data.data.object.list[i].state === 0) {
            data.data.object.list[i].judegState = true
          } else {
            data.data.object.list[i].judegState = false
          }
        }
        this.pages.total = data.data.object.total
        this.tableData = data.data.object.list
      }
    }).catch(err => {
      console.log(err)
    })
  }
}
</script>

<style>
  .el-pagination.is-background .el-pager li:not(.disabled).active {
    background-color: #FD7A3A !important;
    color: #fff !important;
  }
  .el-form-item__label {
    text-align: right !important;
  }
  .cell{
    text-align: center !important;
    overflow: visible !important;
  }
</style>
<style lang="less" scoped>
  #UserManagement{
    width: 100%;
    padding: 15px;
    background-color: rgb(255, 244, 231);
    .stateSpan{
      display: inline-block;
      width: 263px;
    }
    .stateBtn{
      padding:7px 20px !important;
    }
    .pages{
      display: flex;
      justify-content:flex-end;
      flex-wrap:wrap;
    }
    a{
      text-decoration: none;
      color: rgb(255, 149, 18);
      margin-right: 10px;
      font-size: 12px;
      cursor: pointer;
    }
    .arrow{
      position: relative;
      &:hover .more{
        display: block;
      }
      .more{
        width: 132px;
        height: 30px;
        line-height: 30px;
        background-color: #fff;
        border: 1px solid #e6ebf5;
        box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
        border-radius: 5px;
        position: absolute;
        top: -5px;
        right: 0;
        z-index: 999;
        display: none;
      }
    }
    .cover{
      position: fixed;
      top: 0;
      left: 0;
      width: 100vw;
      height: 100vh;
      background-color: rgba(0,0,0,.6);
      z-index: 777;
      .middle{
        width: 500px;
        max-width: 100%;
        height: 500px;
        max-height: 100%;
        background-color: #fff;
        overflow: auto;
        padding: 20px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        .delete{
          width: 25px;
          height: 25px;
          font-size: 20px;
          font-weight: 600;
          cursor: pointer;
          position: absolute;
          top: 0;
          right: 0;
          text-align: center;
          line-height: 20px;
        }
      }
    }
  }
</style>
